package com.huiqiao.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huiqiao.bus.domain.Car;
import com.huiqiao.bus.domain.Check;
import com.huiqiao.bus.domain.Customer;
import com.huiqiao.bus.domain.Rent;
import com.huiqiao.bus.mapper.CarMapper;
import com.huiqiao.bus.mapper.CheckMapper;
import com.huiqiao.bus.mapper.CustomerMapper;
import com.huiqiao.bus.mapper.RentMapper;
import com.huiqiao.bus.service.ICheckService;
import com.huiqiao.bus.vo.CheckVo;
import com.huiqiao.sys.constant.SysConstant;
import com.huiqiao.sys.domain.User;
import com.huiqiao.sys.utils.DataGridView;
import com.huiqiao.sys.utils.RandomUtils;
import com.huiqiao.sys.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.*;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-20  10:27
 */


@Service
@Transactional //事务控制
public class CheckServiceImpl implements ICheckService {

    @Autowired
    private CheckMapper checkMapper;

    @Autowired
    private RentMapper rentMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CarMapper carMapper;


    @Override
    public int saveCheck(CheckVo checkVo) {
        try {
            this.checkMapper.insertSelective(checkVo);
            return 0;
        }catch (Exception e){
            System.out.println("e = " + e);
            return -1;
        }
    }

    //查询所有检查
    @Override
    public DataGridView queryAllCheck(CheckVo checkVo) {
        Page<Object> page = PageHelper.startPage(checkVo.getPage(), checkVo.getLimit());
        Weekend<Check> checkWeekend = Weekend.of(Check.class);
        WeekendCriteria<Check, Object> criteria = checkWeekend.weekendCriteria();
        //查询检查单号
        if (StrUtil.isNotBlank(checkVo.getCheckid())) {
            criteria.andLike(Check::getCheckid, "%" + checkVo.getCheckid() + "%");
        }
        //查询根据开始时间和结束时间查询
        if (Objects.nonNull(checkVo.getStartTime()) && Objects.nonNull(checkVo.getEndTime())) {
            criteria.andBetween(Check::getCheckdate, checkVo.getStartTime(), checkVo.getEndTime());
        }
        //查询检查描述
        if (StrUtil.isNotBlank(checkVo.getCheckdesc())) {
            criteria.andLike(Check::getCheckdesc, "%" + checkVo.getCheckdesc() + "%");
        }
        //查询检查结果
        if (StrUtil.isNotBlank(checkVo.getProblem())) {
            criteria.andLike(Check::getProblem, "%" + checkVo.getProblem() + "%");
        }
        //查询罚款金额
        if (Objects.nonNull(checkVo.getPaymoney())) {
            criteria.andEqualTo(Check::getPaymoney, checkVo.getPaymoney());
        }
        //查询操作员
        if (StrUtil.isNotBlank(checkVo.getOpername())) {
            criteria.andLike(Check::getOpername, "%" + checkVo.getOpername() + "%");
        }
        //查询出租单号
        if (StrUtil.isNotBlank(checkVo.getRentid())) {
            criteria.andLike(Check::getRentid, "%" + checkVo.getRentid() + "%");
        }
        //查询创建时间
        if (Objects.nonNull(checkVo.getCreatetime())) {
            criteria.andEqualTo(Check::getCreatetime, checkVo.getCreatetime());
        }
        List<Check> data = this.checkMapper.selectByExample(checkWeekend);
        return new DataGridView(page.getTotal(), data);
    }

    //删除检查
    @Override
    public int deleteCheck(String checkid) {
        try {
            this.checkMapper.deleteByPrimaryKey(checkid);
            return 0;
        }catch (Exception e){
            System.out.println(e);
            return -1;
        }
    }

    //批量删除检查
    @Override
    public int deleteBatchCheck(String[] ids) {
        try {
            Arrays.asList(ids).stream().forEach(checkMapper::deleteByPrimaryKey);
            return 0;
        }catch (Exception e){
            System.out.println(e);
            return -1;
        }
    }

    //修改检查
    @Override
    public int updateCheck(CheckVo checkVo) {
        try {
            this.checkMapper.updateByPrimaryKeySelective(checkVo);
            return 0;
        }catch (Exception e){
            System.out.println(e);
            return -1;
        }
    }

    @Override
    public Map<String, Object> initCheckFormData(String rentid) {
        //查询出租单
        Rent rent = this.rentMapper.selectByPrimaryKey(rentid);
        //查询客户
        Customer customer =this.customerMapper.selectByPrimaryKey(rent.getIdentity());
        //查询⻋辆
        Car car = this.carMapper.selectByPrimaryKey(rent.getCarnumber());
        //组装check
        Check check = new Check();
        check.setCheckid(RandomUtils.createRandomStringUseTime(SysConstant.CAR_ORDER_JC));
        check.setRentid(rentid);
        check.setCheckdate(new Date());
        User user =(User) WebUtils.getHttpSession().getAttribute("user");
        if(Objects.nonNull(user)){
            check.setOpername(user.getRealname());
        }else {
            check.setOpername("系统管理员");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("rent",rent);
        map.put("customer",customer);
        map.put("car",car);
        map.put("check",check);
        return map;
    }

    @Override
    public void addCheck(CheckVo checkVo) {
        this.checkMapper.insertSelective(checkVo);
        //更改出租单的状态
        Rent rent =this.rentMapper.selectByPrimaryKey(checkVo.getRentid());
        //更改为已归还
        rent.setRentflag(Double.valueOf(SysConstant.RENT_BACK_TRUE));
        this.rentMapper.updateByPrimaryKeySelective(rent);
        //更改汽车的状态
        Car car =this.carMapper.selectByPrimaryKey(rent.getCarnumber());
        //更改汽车状态为未出租
        car.setIsrenting(SysConstant.RENT_CAR_FALSE);
        this.carMapper.updateByPrimaryKeySelective(car);
    }
}
